/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fix.it.source.fixit;

import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.enterprise.context.RequestScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * REST Web Service
 *
 * @author Biserka
 */
@Path("generic")
@RequestScoped
public class GenericResource {

    @PersistenceContext(unitName = "fix.it.source_FixIt_war_1.0-SNAPSHOTPU")
    EntityManager entityManager;
    @Context
    private UriInfo context;

    /**
     * Creates a new instance of GenericResource
     */
    public GenericResource() {
    }
    
    @GET
    @Path("/percentage")
    @Produces("application/xml")
    public String getXml() {
        int sumSolved = 0;
        int sumNotSolved = 0;
        String xmlSolved = "<solvedProblems>";
        String xmlNotSolved = "<notSolvedProblems>";
        Query query1 = entityManager.createQuery("SELECT p from Problem p");
        List<Problem> problems = query1.getResultList();
        for(int i = 0; i< problems.size(); i++){
            Problem currentProblem = problems.get(i);
            if(currentProblem.getIssolved() == 1){
                sumSolved ++;
                xmlSolved += "<problemName>" + currentProblem.getProblemname() +"</problemName>" +
                        "<problemDate>"  + currentProblem.getReportdateString() + "</problemDate>" +
                        "<importance>" + currentProblem.getImportance() + "</importance>";
            }
            else if(currentProblem.getIssolved() == 0){
                sumNotSolved ++;
                xmlNotSolved += "<problemName>" + currentProblem.getProblemname() +"</problemName>" +
                        "<problemDate>"  + currentProblem.getReportdateString() + "</problemDate>" +
                        "<importance>" + currentProblem.getImportance() + "</importance>";
            }
        }        
        /* solved problems ratio */
        if(sumNotSolved > 0) {            
            xmlNotSolved += "<problemsRatio>There are "+ sumNotSolved + " unresolved problems out of " + problems.size() + " and the ratio is " + (double)sumNotSolved/ problems.size() + "</problemsRatio>";
            xmlNotSolved += "</notSolvedProblems>";
            return xmlNotSolved;
        }
        else {
            xmlSolved += "<problemsRatio>All " + problems.size() + " problems are solved!</problemsRatio>";
            xmlSolved += "</solvedProblems>";
            return xmlSolved;
        }
    }

    /**
     * PUT method for updating or creating an instance of GenericResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/xml")
    public void putXml(String content) {
    }
//    @GET
//@Produces("text/html")
//public String getGoogleMap() {
//    try {
//         String address = c.getAddressline1() + " " + c.getAddressline2() + " " +
//                 c.getCity() + " " + c.getState() + " " + c.getZip();
//         java.lang.Integer zoom = 15;
//         String iframe = "false";
// 
//         RestResponse result = GoogleMapService.getGoogleMap(address, zoom, iframe);
//         return result.getDataAsString();
//     
//     } catch (Exception ex) {
//         ex.printStackTrace();
//     } 
//
//     
//     return "";
// }
}
